<!DOCTYPE html>
<html>
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>首页</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="../../js/vue.js"></script>
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="../../js/jquery-3.3.1.min.js"></script>
    <style>
        html body{
            height: 100%;
            margin: 0;
            padding: 0;
        }
    </style>
</head>
<body>
<div id="app">
    <el-dialog
            title="添加经费"
            :visible.sync="dialogVisible"
            width="30%">
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="部门编号">
                <el-input :disabled="true" v-model="form.DEPT_ID"></el-input>
            </el-form-item>
            <el-form-item label="加经费余额">
                <el-input v-model="form.balance"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="update">提交</el-button>
                <el-button @click="dialogVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
    <el-input v-model="deptName" style="width: 150px" placeholder="请输入部门名称"></el-input>
    <el-button type="primary" @click="select" plain>查询</el-button>
    <el-table :data="tableData" style="width: 100%" max-height="500">
        <el-table-column
                fixed
                prop="DEPT_ID"
                label="id"
                width="部门编号">
        </el-table-column>
        <el-table-column
                prop="DEPT_NAME"
                label="部门名称"
                width="300">
        </el-table-column>
        <el-table-column
                prop="BALANCE"
                label="经费余额"
                width="300">
        </el-table-column>
        <el-table-column
                prop="USERNAME"
                label="操作人"
                width="300">
        </el-table-column>
        <el-table-column
                prop="MEMO"
                label="备注"
                width="300">
        </el-table-column>
        </el-table-column>
        <el-table-column
                fixed="right"
                label="操作"
                width="220">
            <template slot-scope="scope">
                <el-button
                        @click.native.prevent="selectRow(scope.$index, tableData)"
                        type="text"
                        size="small">
                    查看
                </el-button>
                <el-button
                        @click.native.prevent="deleteRow(scope.$index, tableData)"
                        type="text"
                        size="small">
                    移除
                </el-button>
                <el-button
                        @click.native.prevent="updateRow(scope.$index, tableData)"
                        type="text"
                        size="small">
                    修改
                </el-button>
            </template>
        </el-table-column>
    </el-table>
    <el-dialog title="部门经费使用情况" :visible.sync="dialogTableVisible">
        <el-table :data="tableDataDept">
            <el-table-column property="USERNAME" label="申请人" width="150"></el-table-column>
            <el-table-column property="APPLY_TIME" label="申请时间" width="200">
                <template slot-scope="scope">
                    {{ new Date(scope.row.APPLY_TIME).toLocaleDateString() }}
                </template>
            </el-table-column>
            <el-table-column property="GOODSNAME" label="商品名称"></el-table-column>
            <el-table-column property="GOODS_COUNT" label="购买数量"></el-table-column>
            <el-table-column property="MONEY" label="购买总金额"></el-table-column>
        </el-table>
    </el-dialog>
</div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            dialogVisible: false,
            dialogTableVisible: false,
            tableData:[],
            tableDataDept:[],
            status:0,
            deptName:'',
            form: {
                DEPT_ID: 0,
                balance: 0
            }
        },
        methods: {
            deleteRow(index, rows) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let vue= this;
                    $.ajax({
                        url:"http://localhost:8080/ssmzy05/deptfunds/delete",
                        type:"post",
                        data:{
                          deptId:rows[index].DEPT_ID
                        },
                        dataType:"json",
                        success:function (data) {
                            if(data==true){
                                alert("存在使用情况的用品不能删除")
                            }else {
                                vue.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                rows.splice(index, 1);
                            }
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            updateRow(index, rows){
                let vue = this;
                vue.dialogVisible = true;
                let row = rows[index]; // 获取指定索引处的数据对象
                vue.form.DEPT_ID = row.DEPT_ID; // 将指定索引处的属性值逐一赋给 vue.form
                vue.status = 2;
            },
            selectRow(index, rows){
                let vue = this;
                vue.dialogTableVisible = true;
                let row = rows[index];
                $.ajax({
                    url:"http://localhost:8080/ssmzy05/deptfunds/selectDeptAll",
                    type:"post",
                    dataType:"json",
                    data:{
                        deptId : row.DEPT_ID
                    },
                    success:function (data) {
                        vue.tableDataDept=data;
                    }
                })
            },
            select(){
                let vue= this;
                $.ajax({
                    url:"http://localhost:8080/ssmzy05/deptfunds/select",
                    type:"post",
                    dataType:"json",
                    data:{
                        deptName : vue.deptName
                    },
                    success:function (data) {
                        vue.tableData=data;
                    }
                })
            },
            update(){
                let _this = this;
                $.ajax({
                    url:"http://localhost:8080/ssmzy05/deptfunds/update",
                    type:"post",
                    data: _this.form,
                    dataType:"json",
                    success:function (data) {
                        _this.select()
                        _this.dialogVisible = false;
                        _this.form.balance = 0;
                    }
                })
            }
        },
        mounted() {
            this.select();
        },
        watch: {
            dialogVisible(newValue) {
                if (!newValue) {
                    this.form = {
                       classname: '',
                        remark: ''
                    };
                }
            }
        },
    })
</script>
</html>